package com.xiaobias.distributed.pan.db.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaobias.distributed.pan.db.entity.BaiduUser;
import com.xiaobias.distributed.pan.db.mapper.BaiduUserMapper;
import com.xiaobias.distributed.pan.db.service.BaiduUserService;
import com.xiaobias.distributed.pan.query.UserQuery;
import com.xiaobias.distributed.pan.web.vo.UserVo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class BaiduUserServiceImpl extends ServiceImpl<BaiduUserMapper, BaiduUser> implements BaiduUserService {

    @Resource
    private BaiduUserMapper baiduUserMapper;

    @Override
    public IPage<UserVo> page(UserQuery query) {
        final Integer page = query.getPage();
        final Integer size = query.getSize();
        QueryWrapper<BaiduUser> wrapper = new QueryWrapper<>();
        IPage<BaiduUser> iPage = baiduUserMapper.selectPage(new Page<>(page, size), wrapper);
        return iPage.convert(baiduUser -> {
            UserVo userVo = new UserVo();
            userVo.setId(baiduUser.getId());
            userVo.setAccount(baiduUser.getToken());
            userVo.setUsername(baiduUser.getBaiduName());
            return userVo;
        });
    }
}